<template>
  <div>
    <el-form :model="msg.data" :rules="rules" ref="form" label-position="left" label-width="30%" class="ml30">
      <el-row>
        <el-col :span="12">
          <el-form-item label="学籍号：" prop="schoolRollId">
            <el-input class="width80" v-model="msg.data.schoolRollId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名：" prop="name">
            <el-input class="width80" v-model="msg.data.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="性别：" prop="sex">
            <el-select class="width80" v-model="msg.data.sex" placeholder="性别">
              <el-option label="男" value="男"/>
              <el-option label="女" value="女"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="民族：" prop="nation">
            <el-select class="width80" v-model="msg.data.nation" placeholder="民族">
              <el-option v-for="(value, key) in nation" v-bind:key="key" :label="key" :value="value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="出生日期：" prop="birthday">
            <el-date-picker v-model="msg.data.birthday" type="date" class="width80" placeholder="选择出生日期"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号：" prop="identityId">
            <el-input class="width80" v-model="msg.data.identityId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="入学年级：" prop="gradeYear">
            <el-select class="width80" v-model="msg.data.gradeYear" placeholder="入学年级">
              <el-option v-for="item in gradeYear" v-bind:key="item.key" :label="item.value" :value="item.key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年级：" prop="grade">
            <el-select v-if="/^1[123456]$/.test(msg.data.grade)" class="width80" v-model="msg.data.grade" placeholder="年级">
              <el-option label="一年级" value="11"/>
              <el-option label="二年级" value="12"/>
              <el-option label="三年级" value="13"/>
              <el-option label="四年级" value="14"/>
              <el-option label="五年级" value="15"/>
              <el-option label="六年级" value="16"/>
            </el-select>
            <el-select v-if="/^2[123]$/.test(msg.data.grade)" class="width80" v-model="msg.data.grade" placeholder="年级">
              <el-option label="初一" value="21"/>
              <el-option label="初二" value="22"/>
              <el-option label="初三" value="23"/>
            </el-select>
            <el-select v-if="/^3[123]$/.test(msg.data.grade)" class="width80" v-model="msg.data.grade" placeholder="年级">
              <el-option label="高一" value="31"/>
              <el-option label="高二" value="32"/>
              <el-option label="高三" value="33"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="班级：" prop="class">
            <el-select class="width80" v-model="msg.data.class" placeholder="班级">
              <el-option v-for="item in classes" v-bind:key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学号：" prop="studentId">
            <el-input class="width80" v-model="msg.data.studentId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="家长电话：" prop="phone">
            <el-input class="width80" v-model="msg.data.phone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="家庭住址：" prop="address">
            <el-input class="width80" v-model="msg.data.address" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="footer">
      <el-button class="el-button--blue" type="primary" @click="submitStudentChange">保 存</el-button>
      <el-button @click="msg.visible=false">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { nation, awaitCheckSchoolRollId, awaitCheckStudentPhone, SubmitStudentChange } from '../../../api/form'
export default {
  name: 'student-change',
  model: {
    prop: 'msg',
    event: 'cc'
  },
  props: {
    msg: {}
  },
  mounted () {
    for (let i = 2000; i <= new Date().getFullYear(); i++) {
      this.gradeYear.push({ key: i, value: i + '级' })
    }
    for (let i = 1; i < 100; i++) {
      this.classes.push(i)
    }
  },
  data () {
    let validateSchoolRollId = (rule, value, callback) => {
      if (value.toString() !== '') {
        let res = awaitCheckSchoolRollId(this.msg.data.schoolRollId, this.msg.data.id)
        if (res.status === 200) {
          if (!res.responseJSON.result) {
            callback(new Error(res.responseJSON.msg))
          } else {
            callback()
          }
        }
      }
      callback()
    }
    let validatePhone = (rule, value, callback) => {
      if (value.toString() !== '') {
        let res = awaitCheckStudentPhone(this.msg.data.phone, this.msg.data.id)
        if (res.status === 200) {
          if (!res.responseJSON.result) {
            callback(new Error(res.responseJSON.msg))
          } else {
            callback()
          }
        }
      }
      callback()
    }
    return {
      gradeYear: [],
      classes: [],
      rules: {
        schoolRollId: [
          { required: true, message: '学籍号不能为空', trigger: 'change' },
          { min: 1, max: 30, message: '长度在 30 个字符以内', trigger: 'change' },
          { validator: validateSchoolRollId, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'change' },
          { min: 1, max: 10, message: '长度在 10 个字符以内', trigger: 'change' }
        ],
        identityId: [{ min: 0, max: 18, message: '长度在 18 个字符以内', trigger: 'change' }],
        studentId: [{ min: 1, max: 30, message: '长度在 30 个字符以内', trigger: 'change' }],
        phone: [
          { type: 'string', pattern: /(^([0-9]{3,4}-)?[0-9]{7,8}$)|(^0?1[3|4|5|8][0-9]\d{8}$)/, message: '非法电话号码', trigger: 'change' },
          { type: 'string', pattern: /(^([0-9]{3,4}-)?[0-9]{7,8}$)|(^0?1[3|4|5|8][0-9]\d{8}$)/, message: '非法电话号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        address: [{ min: 0, max: 255, message: '长度在 255 个字符以内', trigger: 'change' }]
      },
      nation: nation
    }
  },
  methods: {
    submitStudentChange () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          SubmitStudentChange(this.msg.data).then(res => {
            this.msg.visible = false
            this.$store.dispatch('SetFlash')
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .width80 {
    width: 80%;
  }
  .footer {
    display: flex;
    justify-content: flex-end;
  }
</style>
